package com.hdax.dm.service;

import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.dto.item.FloorDto;
import com.hdax.dm.dto.item.FloorItemDto;
import com.hdax.dm.dto.item.HotItemDto;
import com.hdax.dm.dto.item.ItemDescDTO;
import com.hdax.dm.entity.base.DmImage;
import com.hdax.dm.entity.item.DmCinema;
import com.hdax.dm.entity.item.DmItem;
import com.hdax.dm.entity.item.DmItemType;
import com.hdax.dm.feign.base.DmImageFeign;
import com.hdax.dm.feign.item.DmCinemaFeign;
import com.hdax.dm.feign.item.DmItemFeign;
import com.hdax.dm.feign.item.DmItemTypeFeign;
import com.hdax.dm.utils.MokeImage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DmItemService {

    @Resource
    private DmItemFeign dmItemFeign;
    @Resource
    private DmImageFeign dmImageFeign;
    @Resource
    private DmItemTypeFeign dmItemTypeFeign;
    @Resource
    private DmCinemaFeign cinemaFeign;

    //根据itemid查询商品信息
    public CommonResponse<ItemDescDTO> getItems(Long id){
        //拷贝属性
        DmItem dmItem = dmItemFeign.getItems(id);
        ItemDescDTO itemDescDTO = new ItemDescDTO();
        BeanUtils.copyProperties(dmItem,itemDescDTO);
        //详情信息需要去掉 "\\"
        itemDescDTO.setBasicDescription(dmItem.getBasicDescription().replace("\\",""));
        itemDescDTO.setProjectDescription(dmItem.getProjectDescription().replace("\\",""));
        itemDescDTO.setReminderDescription(dmItem.getReminderDescription().replace("\\",""));
        DmImage dmImage = dmImageFeign.getImage(2L,1L,id);
        itemDescDTO.setImgUrl(dmImage == null ? MokeImage.getImage(1) :dmImage.getImgUrl());
        return DtoUtil.returnSuccess(itemDescDTO);
    }

    //查询今日推荐
    public CommonResponse<List<HotItemDto>> recommend(){
        //拷贝属性
        List<DmItem> dmItems = dmItemFeign.recommend();
        List<HotItemDto> hotItemDtos = new ArrayList<>();
        dmItems.forEach(dmItem -> {
            HotItemDto hotItemDto = new HotItemDto();
            BeanUtils.copyProperties(dmItem,hotItemDto);
            //设置图片
            DmImage dmImage = dmImageFeign.getImage(2L,1L,dmItem.getId());
            hotItemDto.setImgUrl(dmImage == null ? MokeImage.getImage(1):dmImage.getImgUrl());
            hotItemDtos.add(hotItemDto);
        });
        return DtoUtil.returnSuccess(hotItemDtos);
    }

    //查询即将开售
    public CommonResponse<List<HotItemDto>> sell(){
        //拷贝属性
        List<DmItem> dmItems = dmItemFeign.sell();
        List<HotItemDto> hotItemDtos = new ArrayList<>();
        dmItems.forEach(dmItem -> {
            HotItemDto hotItemDto = new HotItemDto();
            BeanUtils.copyProperties(dmItem,hotItemDto);
            //设置图片
            DmImage dmImage = dmImageFeign.getImage(2L,1L,dmItem.getId());
            hotItemDto.setImgUrl(dmImage == null ? MokeImage.getImage(1):dmImage.getImgUrl());
            hotItemDtos.add(hotItemDto);
        });
        return DtoUtil.returnSuccess(hotItemDtos);
    }

    //查询楼层及其对应热门商品
    public CommonResponse<List<FloorDto>> floor(){
        List<DmItemType> dmItemTypes = dmItemTypeFeign.getItemTypes(0L);
        List<FloorDto> floorDtos = new ArrayList<>();
        //拷贝楼层信息
        dmItemTypes.forEach(dmItemType -> {
            FloorDto floorDto = new FloorDto();
            BeanUtils.copyProperties(dmItemType,floorDto);
            floorDto.setIndex(dmItemType.getId());
            floorDto.setItemTypeId(dmItemType.getId());
            floorDto.setItemTypeName(dmItemType.getItemType());
            List<DmItem> dmItems = dmItemFeign.hotItems(dmItemType.getId());
            List<FloorItemDto> floorItemDtos = new ArrayList<>();
            //拷贝楼层商品信息
            dmItems.forEach(dmItem -> {
                FloorItemDto floorItemDto = new FloorItemDto();
                BeanUtils.copyProperties(dmItem,floorItemDto);
                //查询图片
                DmImage dmImage = dmImageFeign.getImage(2L,1L,dmItem.getId());
                floorItemDto.setImgUrl(dmImage == null ? MokeImage.getImage(1):dmImage.getImgUrl());
                //查询地址
                DmCinema dmCinema = cinemaFeign.getCinema(dmItem.getCinemaId());
                floorItemDto.setAreaId(dmCinema.getId());
                floorItemDto.setAreaName(dmCinema.getAreaName());
                floorItemDto.setAddress(dmCinema.getAddress());
                floorItemDtos.add(floorItemDto);
            });
            floorDto.setItems(floorItemDtos);
            floorDtos.add(floorDto);
        });
        return DtoUtil.returnSuccess(floorDtos);
    }

    //查询轮播图
    public List<DmImage> carousel(){
        return dmImageFeign.carousel();
    }

    //楼层热门演出排行
    public CommonResponse<List<FloorItemDto>> seniority(Long typeId){
        //拷贝属性
        List<FloorItemDto> floorItemDtos = new ArrayList<>();
        List<DmItem> dmItems = dmItemFeign.getFloorOrder(typeId,10L);
        dmItems.forEach(dmItem -> {
            FloorItemDto floorItemDto = new FloorItemDto();
            BeanUtils.copyProperties(dmItem,floorItemDto);
            //查询地址
            DmCinema dmCinema = cinemaFeign.getCinema(dmItem.getCinemaId());
            floorItemDto.setAreaId(dmCinema.getId());
            floorItemDto.setAreaName(dmCinema.getAreaName());
            floorItemDtos.add(floorItemDto);
        });
        return DtoUtil.returnSuccess(floorItemDtos);
    }

    //商品详情热门推荐
    public CommonResponse<List<FloorItemDto>> getRecommend(Long typeId){
        //拷贝属性
        List<FloorItemDto> floorItemDtos = new ArrayList<>();
        List<DmItem> dmItems = dmItemFeign.hotItems(typeId);
        dmItems.forEach(dmItem -> {
            FloorItemDto floorItemDto = new FloorItemDto();
            BeanUtils.copyProperties(dmItem,floorItemDto);
            //查询图片
            DmImage dmImage = dmImageFeign.getImage(2L,1L,dmItem.getId());
            floorItemDto.setImgUrl(dmImage == null ? MokeImage.getImage(1):dmImage.getImgUrl());
            //查询地址
            DmCinema dmCinema = cinemaFeign.getCinema(dmItem.getCinemaId());
            floorItemDto.setAreaId(dmCinema.getId());
            floorItemDto.setAreaName(dmCinema.getAreaName());
            floorItemDto.setAddress(dmCinema.getAddress());
            floorItemDtos.add(floorItemDto);
        });
        return DtoUtil.returnSuccess(floorItemDtos);
    }

}
